﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class admin_qlNews : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        e.Cancel = true;
        Label id = (Label)this.GridView1.Rows[e.NewEditIndex].Cells[0].FindControl("id");
        Label lang = (Label)this.GridView1.Rows[e.NewEditIndex].Cells[1].FindControl("lang");
        Response.Redirect("Default.aspx?page=ednews&id=" + id.Text + "&lang=" + lang.Text);
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        e.Cancel = true;
        Response.Write(Common.confirmDelete(null));
        Label id = (Label)this.GridView1.Rows[e.RowIndex].Cells[5].FindControl("imageUrl");
        String path = "~/image/";
        DeleteImage(path + id.Text);
        e.Cancel = false;
    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        SqlDataSource2.DataBind();
    }
    protected void addbt_Click(object sender, EventArgs e)
    {

        String regex = "";
        String picPath = "";
        long name = (long)Common.ToSeconds(DateTime.Now);
        if (fulImage.PostedFile.ContentLength > 2097151)
        {
            //info.Visible = true;
            lbImage.Text = "Tập Tin Hình Ảnh Có Dung Lượng Nhỏ Hơn 2MB!";
            //return;
        }
        else
        {
            regex = System.IO.Path.GetExtension(fulImage.PostedFile.FileName.ToLower().ToString());
            picPath = "~/image/" + name + regex;
            if (regex == ".bmp" || regex == ".jpg" || regex == ".gif" || regex == ".png")
            {
                
            }
            else
            {
                //info.Visible = true;
                lbImage.Text = "Tập Tin Hình Ảnh Có Phần Mở Rộng là .jpg,.bmp,.gif,.png!";
            }
        }
        if (regex == "")
        {
            //url.Value = null;
        }
        else
        {
            
            
                
            //Common.resizeImage(name+regex, 800, 300, fulImage);
            fulImage.SaveAs(Server.MapPath(picPath));
            hdfImage.Value = Math.Abs(name).ToString() + regex;
            //url.Value = picPath;
        }
        Response.Write(name.ToString());
        hdfContent.Value = fckContent.Value;
        hdfNewID.Value = Math.Abs(name).ToString();
        SqlDataSource2.Insert();
        SqlDataSource2.DataBind();
        Response.Write("<script>alert('Insert Thành Công')</script>");
        title.Text = "";
        fckContent.Value = "";
        fckContent.Value = "";
        hdfNewID.Value = "";
        des.Text = "";
        //Response.Redirect(Request.Url.ToString());
    }
    private void DeleteImage(String imageName)
    {
        String path = this.Server.MapPath(imageName);
        try
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script language='javascript'>alert('" + ex + "');</script>");
        }
    }
}